//
//  CitiesViewController.m
//  UI_省市区
//
//  Created by 才诗琦 on 15/12/22.
//  Copyright © 2015年 www.lanou3g.com. All rights reserved.
//

#import "CitiesViewController.h"
#import "NSString+Characters.h"
@interface CitiesViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSDictionary *ca;

@property(nonatomic, retain)NSMutableDictionary *cDic ;

@property(nonatomic, retain)NSMutableArray *kd;
@property(nonatomic, retain)NSMutableDictionary *TDic ;
@property(nonatomic, retain)NSMutableArray *dataArr;

@property(nonatomic, retain)NSMutableArray *CArr;

@end

@implementation CitiesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self data];
}

- (void)data
{
    
   
    
    
   
    
//        NSMutableArray *cArr = [NSMutableArray array];
    
         self.ca = self.dit;
    
    self.kd = self.karr;
    
//    }
    self.CArr = [NSMutableArray array];
     for (NSString *temp in _kd ) {
//         if ([temp hasPrefix:@"  "] &&![temp hasPrefix:@"    "]) {
             self.cDic = [NSMutableDictionary dictionary];
             [_cDic setObject:temp forKey:@"CName"];
             [_cDic setObject:[NSMutableArray array] forKey:@"zones"];
            
            _CArr = [_ca objectForKey:@"cities"];
             [_CArr addObject:_cDic];
//         }
     }
    
    self.TDic = [NSMutableDictionary dictionary];
    
    for (NSDictionary *dic in _CArr) {
        
        NSString *captialLetter = [dic[@"CName"] firstCharacterOfName];
        NSMutableArray *Arr = _TDic[captialLetter];
        
        if (Arr) {
            [Arr addObject:dic];
        }
        else{
            Arr = [NSMutableArray array];
            [Arr addObject:dic];
            [_TDic setObject:Arr forKey:captialLetter];
        }
        
        
    } ;
    
    self.dataArr = [[_TDic.allKeys sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
    

    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _TDic.count;
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _dataArr[section];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = _dataArr[section];
    NSArray *arr = _TDic[key];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NSString *key = _dataArr[indexPath.section];
    NSArray *arr = _TDic[key];
    
    NSDictionary *dic = arr[indexPath.row];
    
    
    
    cell.textLabel.text = dic[@"CName"];
    return cell;
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
